package com.shsoft.task;

import com.shsoft.entity.HardWareInfo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class GpuTask implements Runnable {
    @Override
    public void run() {
        while (true){
            try {
                Process process = new ProcessBuilder("nvidia-smi",
                        "--query-gpu=name,temperature.gpu,utilization.gpu,memory.used,memory.free,memory.total,fan.speed,clocks.current.graphics,power.draw",
                        "--format=csv").start();
                String line;
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    String[] fields = line.split(",");
                    if (!line.contains("temperature.gpu")){
                        String name = fields[0];
                        String temperature = fields[1].trim();
                        String utilization = fields[2].replace("%", "").trim();
                        int usedMemory = Integer.parseInt(fields[3].replace("MiB","").trim());
                        int freeMemory = Integer.parseInt(fields[4].replace("MiB","").trim());
                        int totalMemory = Integer.parseInt(fields[5].replace("MiB","").trim());
                        String fan = fields[6].replace("%", "").trim();
                        String freq = fields[7].replace("MHz", "").trim();
                        String power = fields[8].replace("W", "").trim();
                        HardWareInfo.gpuName = name;
                        HardWareInfo.gpuTempCur = Integer.parseInt(temperature);
                        HardWareInfo.gpuUsageCur = Integer.parseInt(utilization);
                        HardWareInfo.gpuUsedMemory = usedMemory;
                        HardWareInfo.gpuFreeMemory = freeMemory;
                        HardWareInfo.gpuTotalMemory = totalMemory;
                        HardWareInfo.gpuClockCur = Integer.parseInt(freq);
                        if(!fan.equals("[N/A]")){
                            HardWareInfo.gpuFanSpeed = Integer.parseInt(fan);
                        }
                        if(!power.equals("[N/A]")){
                            HardWareInfo.gpuPowerCur = Float.parseFloat(power);
                        }
                        // 刷新最大值、最小值
                        if(HardWareInfo.gpuTempMin == 0 || HardWareInfo.gpuTempMin > HardWareInfo.gpuTempCur){
                            HardWareInfo.gpuTempMin = HardWareInfo.gpuTempCur;
                        }
                        if(HardWareInfo.gpuTempMax == 0 || HardWareInfo.gpuTempMax < HardWareInfo.gpuTempCur){
                            HardWareInfo.gpuTempMax = HardWareInfo.gpuTempCur;
                        }
                        if(HardWareInfo.gpuPowerMin == 0 || HardWareInfo.gpuPowerMin > HardWareInfo.gpuPowerCur){
                            HardWareInfo.gpuPowerMin = HardWareInfo.gpuPowerCur;
                        }
                        if(HardWareInfo.gpuPowerMax == 0 || HardWareInfo.gpuPowerMax < HardWareInfo.gpuPowerCur){
                            HardWareInfo.gpuPowerMax = HardWareInfo.gpuPowerCur;
                        }
                        if(HardWareInfo.gpuClockMin == 0 || HardWareInfo.gpuClockMin > HardWareInfo.gpuClockCur){
                            HardWareInfo.gpuClockMin = HardWareInfo.gpuClockCur;
                        }
                        if(HardWareInfo.gpuClockMax == 0 || HardWareInfo.gpuClockMax < HardWareInfo.gpuClockCur){
                            HardWareInfo.gpuClockMax = HardWareInfo.gpuClockCur;
                        }
                        if(HardWareInfo.gpuUsageMin == 0 || HardWareInfo.gpuUsageMin > HardWareInfo.gpuUsageCur){
                            HardWareInfo.gpuUsageMin = HardWareInfo.gpuUsageCur;
                        }
                        if(HardWareInfo.gpuUsageMax == 0 || HardWareInfo.gpuUsageMax < HardWareInfo.gpuUsageCur){
                            HardWareInfo.gpuUsageMax = HardWareInfo.gpuUsageCur;
                        }
                    }
                }
                Thread.sleep(1600);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
